import * as type from './type'
import http from 'utils/http'
const actions = {
          async getShopList ( { commit }) {
            //axios请求数据
            const res = await http({
              url: 'http://localhost:3000/shop'
            })
            const action = {
              type: type.GET_SHOP_LIST,
              payload: res.data
            }
            commit( action )
          },
  async deleteShopListItem ( { commit },id ) {
    //axios请求数据
    const res = await http({
      url: 'http://localhost:3000/shop',
      method: 'DELETE',
      data: {
        id
      }
    })
    console.log( 'res',res )
    const action = {
      type: type.DELETE_SHOP_LIST,
      payload: res.data.result
    }
    commit( action )
  },
  async getShopAddress ( { commit },address ) {
    //axios请求数据
    const res = await http({
      url: '/v1/pois',
      params: {
        type: 'search',
        city_id: 2,
        keyword: address
      }
    })
    const action = {
      type: type.GET_SHOP_ADDRESS,
      payload: res.data
    }
    commit( action )
  },
  async getShopCategory ( { commit } ) {
    //axios请求数据
    const res = await http({
      url: '/shopping/v2/restaurant/category'
    })
    const action = {
      type: type.GET_SHOP_CATEGORY,
      payload: res.data
    }
    commit( action )
  },
  async addShopList ( { commit }, data  ) {
    //axios请求数据
    let params = new URLSearchParams()
    params.append( 'shop_name',data.shop_name )
    params.append( 'shop_address',data.shop_address )
    params.append( 'shop_phone_number',data.shop_phone_number )
    params.append( 'shop_info',data.shop_info )
    params.append( 'shop_slogan',data.shop_slogan )
    params.append( 'shop_category',data.shop_category )
    params.append( 'shop_feature',data.shop_feature )
    params.append( 'delivery_cost',data.delivery_cost )
    params.append( 'shop_hours',data.shop_hours )
    
    const res = await http({
      url: 'http://localhost:3000/shop',
      method: 'POST',
      data: params,
      headers: {
        'Content-Type': 'multipart/form-data'        
      }
    })
    const action = {
      type: type.ADD_SHOP_LIST,
      payload: res.data
    }
    commit( action )
  }
}

export default actions 